package br.cin.ufpe.in1118.middleware.distribution.message;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

import br.cin.ufpe.in1118.middleware.distribution.exceptions.UnmarshallException;

public abstract class Unmarshaller <T extends Serializable> {
	
	@SuppressWarnings("unchecked")
	public T unmarshal(byte[] bytes) throws UnmarshallException{
		try {
			T ret = null;
			
			ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
			ObjectInputStream objectStream = new ObjectInputStream(byteStream);
			
			ret = (T) objectStream.readObject();
			
			return ret;
		} catch (IOException e) {
			throw new UnmarshallException(e);
		} catch (ClassNotFoundException e) {
			throw new UnmarshallException(e);
		}
	}
}
